package com.lebang.controller.sys.cences;

import com.alibaba.fastjson.JSONObject;
import com.lebang.common.Login;
import com.lebang.domain.CencesReqAdCount;
import com.lebang.domain.TaskRouse;
import com.lebang.example.CencesReqAdCountExample;
import com.lebang.service.NewRouseService;
import com.lebang.service.cences.CencesReqAdCountService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;


/**
 * @author ljx
 * @date 2020-12-18
 */
@Controller
@RequestMapping("/sys/cencesreqadcount")
public class CencesReqAdCountController {
    @Autowired
    private CencesReqAdCountService baseService;
    @Autowired
    private NewRouseService newRouseService;


    @Login
    @GetMapping("/list")
    public String list(Model model,HttpSession session) {
        List<TaskRouse> rouses = newRouseService.selectAll();
        rouses = newRouseService.checkRoot(rouses,session);
        model.addAttribute("item", rouses);
        return "/cencesreqadcount/list";
    }

    @Login
    @GetMapping("/add")
    public String add(Integer id, Model model) {
        CencesReqAdCount vo;
        if (id != null) {
            vo = baseService.selectById(id);
        } else {
            vo = new CencesReqAdCount();
        }
        model.addAttribute("item", vo);
        return "/cencesreqadcount/edit";
    }


    @Login
    @PostMapping("/edit")
    @ResponseBody
    public JsonData edit(CencesReqAdCount vo) {
        if (vo.getId() != null) {
            vo.setModifyTime(LocalDateTime.now());
            baseService.updateById(vo);
        } else {
            vo.setCreateTime(LocalDateTime.now());
            baseService.insertModel(vo);
        }
        return Callback.success("success", "操作成功");
    }

    @Login
    @PostMapping("/delete")
    @ResponseBody
    public JsonData delete(Integer id) {
        baseService.deleteById(id);
        return Callback.success("success", "操作成功");
    }

    @Login
    @ApiOperation(value = "分页")
    @PostMapping("/list")
    @ResponseBody
    public JSONObject list(HttpServletRequest request, @RequestParam Integer page, @RequestParam Integer limit,
                           @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime star,
                           @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime end, Integer status,String sdkKey) {
        CencesReqAdCountExample example = new CencesReqAdCountExample();
        example.setOffset((page - 1) * limit.longValue());
        example.setLimit(limit);
        example.setOrderByClause("create_time desc");
        CencesReqAdCountExample.Criteria criteria = example.createCriteria();
        if (status != null) {
            criteria.andStatusEqualTo(status);
        }
        if (star != null || end != null) {
            criteria.andCreateTimeBetween(star, end);
        }
        if (!TextUtil.isEmpty(sdkKey)){
            criteria.andSdkKeyEqualTo(sdkKey);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("data", baseService.selectByExample(example));
        jsonObject.put("count", baseService.countByExample(example));
        return jsonObject;
    }
}
